package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.BackgroundInvestigation;
import com.amsp.oa.oademo.service.BackgroundInvestigationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/background-investigation")
@Api(tags = "背景调查表管理")
public class BackgroundInvestigationController {

    @Autowired
    private BackgroundInvestigationService backgroundInvestigationService;

    @PostMapping("/save")
    @ApiOperation("保存背景调查表")
    public ResponseEntity<?> save(@RequestBody BackgroundInvestigation backgroundInvestigation) {
        try {
            log.info("接收到的背景调查表数据: {}", backgroundInvestigation);

            // 基本数据验证
            if (backgroundInvestigation.getName() == null || backgroundInvestigation.getName().trim().isEmpty()) {
                return ResponseEntity.badRequest().body("姓名不能为空");
            }
            if (backgroundInvestigation.getAppliedPosition() == null
                    || backgroundInvestigation.getAppliedPosition().trim().isEmpty()) {
                return ResponseEntity.badRequest().body("应聘职位不能为空");
            }

            boolean result = backgroundInvestigationService.saveOrUpdate(backgroundInvestigation);
            if (result) {
                return ResponseEntity.ok(true);
            } else {
                return ResponseEntity.badRequest().body("保存失败");
            }
        } catch (Exception e) {
            log.error("保存背景调查表失败", e);
            return ResponseEntity.badRequest().body("保存失败: " + e.getMessage());
        }
    }

    @GetMapping("/view/{id}")
    @ApiOperation("查看背景调查表详情")
    public ResponseEntity<?> getById(@PathVariable Long id) {
        try {
            BackgroundInvestigation record = backgroundInvestigationService.getById(id);
            if (record != null) {
                return ResponseEntity.ok(record);
            } else {
                return ResponseEntity.badRequest().body("未找到记录");
            }
        } catch (Exception e) {
            log.error("获取背景调查表失败", e);
            return ResponseEntity.badRequest().body("获取失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation("查询背景调查表列表")
    public ResponseEntity<?> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String appliedPosition) {
        try {
            Page<BackgroundInvestigation> page = new Page<>(current, size);
            QueryWrapper<BackgroundInvestigation> queryWrapper = new QueryWrapper<>();

            if (name != null && !name.isEmpty()) {
                queryWrapper.like("name", name);
            }
            if (appliedPosition != null && !appliedPosition.isEmpty()) {
                queryWrapper.like("applied_position", appliedPosition);
            }

            queryWrapper.orderByDesc("create_time");
            return ResponseEntity.ok(backgroundInvestigationService.page(page, queryWrapper));
        } catch (Exception e) {
            log.error("查询背景调查表列表失败", e);
            return ResponseEntity.badRequest().body("查询失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除背景调查表")
    public ResponseEntity<?> delete(@PathVariable Long id) {
        try {
            boolean result = backgroundInvestigationService.removeById(id);
            if (result) {
                return ResponseEntity.ok(true);
            } else {
                return ResponseEntity.badRequest().body("删除失败");
            }
        } catch (Exception e) {
            log.error("删除背景调查表失败", e);
            return ResponseEntity.badRequest().body("删除失败: " + e.getMessage());
        }
    }
}